package com.renwei.yunlong.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baidu.mapapi.search.core.PoiInfo;
import com.renwei.yunlong.R;
import com.renwei.yunlong.utils.StringUtils;
import com.renwei.yunlong.utils.UtilList;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Project :  yunna.
 * Package name: com.renwei.yunlong.adapter
 * Created by :  Benjamin.
 * Created time: 2018/1/17 15:02
 * Changed by :  Benjamin.
 * Changed time: 2018/1/17 15:02
 * Class description:
 */

public class PoiListAdapter extends RecyclerView.Adapter<PoiListAdapter.ViewHolder> {

    private Context context;
    private List<PoiInfo> poiList;
    public PoiSelectListener listener;
    private String poiId = "";


    public interface PoiSelectListener {
        void poiSelect(PoiInfo detail);
    }


    public void setListener(PoiSelectListener listener) {
        this.listener = listener;
    }

    public PoiListAdapter(Context context) {
        this.context = context;
    }

    public void refresh(List<PoiInfo> poiList) {
        this.poiList = poiList;
        if (UtilList.getCount(poiList)>0)
        {
            this.poiId= StringUtils.value(poiList.get(0).uid);
        }
        notifyDataSetChanged();
    }


    public void changeSelectItem(String poiId) {
        this.poiId = poiId;
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_poi, parent, false);
        return new ViewHolder(view);

    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final PoiInfo poi = poiList.get(position);
        holder.poiName.setText(poi.name);
        holder.poiDetail.setText(poi.address);
        if (poiId.equals(String.valueOf(poi.uid))) {
            holder.icCheck.setImageResource(R.mipmap.check02);
        } else {
            holder.icCheck.setImageResource(R.mipmap.check03);
        }
        holder.poiParent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.poiSelect(poi);
            }
        });
    }

    @Override
    public int getItemCount() {
        return UtilList.getCount(poiList);
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.ic_check)
        ImageView icCheck;
        @Bind(R.id.poi_name)
        TextView poiName;
        @Bind(R.id.poi_detail)
        TextView poiDetail;
        @Bind(R.id.poi_parent)
        LinearLayout poiParent;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }
}
